#include<stdio.h>
#include<string.h>

int intcmp(void*,void*);
void *isearch(void*,void*,int,int,int(*intcmp)(void*,void*));
int Strcmp(void*,void*);

int main(){
	int array[] = {2,3,4,8,5,4,9};
	char* sarray[] = {"abc","efg","hhh"};
	char* starget = "efg";
	int target = 8;
	int length = 7;
	int *location = isearch(&target,array,length,sizeof(int),intcmp);
	char **slocation = isearch(&starget,sarray,3,sizeof(char*),Strcmp);
	if(location == NULL){
		printf("not find!");
	}
	else{
		printf("%p,%d\n",location,*location);
	}
	if(slocation == NULL)
		printf("not find!");
	else
		printf("%p,%s\n",slocation,*slocation);
}

int intcmp(void *elem1,void *elem2){
	int *ie1 = elem1;
	int *ie2 = elem2;
	return *ie1-*ie2;
}

int Strcmp(void *elem1,void *elem2){
	char *e1 = *(char**)elem1;
	char *e2 = *(char**)elem2;
	return strcmp(e1,e2);
}

void *isearch(void *target,void *array,int length,int elementsize,int (*intcmp)(void *,void *)){
	for(int i=0;i<length;i++){
		void *elementAddr = (char*)array+i*elementsize;
		if(intcmp(target,elementAddr)==0)
			return elementAddr;
	}
	return NULL;
}
